package cn.usian.jyzx.order.listener;


import cn.usian.jyzx.order.mapper.OrderInfoMapper;
import cn.usian.jyzx.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 11 12 14 57
 **/

@Slf4j
@RabbitListener(queuesToDeclare = @Queue("order-pay-queue"))
@Component
public class PayListener {




    @Autowired(required = false)
    OrderInfoMapper orderInfoMapper;



    @RabbitHandler
    public void receMessage(String outTradeNo, Message message, Channel channel) throws IOException {

        try{
            log.info("==================================receMessage:{}",outTradeNo);

            int i = orderInfoMapper.updateOrderStatus(outTradeNo, 1);

            if(i>0){
                /**
                 * ACK，用的最多的一种
                 * deliveryTag:该消息的index
                 * false:表示不是批量
                 */
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            }

        }catch (Exception e){
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);

        }




    }

//
//    @RabbitListener(queuesToDeclare = @Queue("order-pay-queue"))
//    @RabbitHandler
//    public void receMessage1(List outTradeNo, Message message, Channel channel) throws IOException {
//
//        try{
//            log.info("receMessage:{}",outTradeNo);
//
//            int i = orderInfoMapper.updateOrderStatus(outTradeNo, 1);
//
//            if(i>0){
//                /**
//                 * ACK，用的最多的一种
//                 * deliveryTag:该消息的index
//                 * false:表示不是批量
//                 */
//                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//            }
//
//        }catch (Exception e){
//            e.printStackTrace();
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
//
//        }
//
//
//
//
//    }
}
